package january.Test_1_28;

import java.util.Stack;

public class Parenthesis {
    public boolean chkParenthesis(String A, int n) {
        // write code here
        Stack<Character> stack = new Stack<>();
        for(int i = 0; i < n; i++) {
            if(A.charAt(i) == '(') {
                stack.push('(');
            } else if(A.charAt(i) == ')') {
                if(stack.empty() && stack.pop() != '(') {
                    return false;
                }
            } else {
                return false;
            }
        }
        if(!stack.empty()) {
            return false;
        }
        return true;
    }
}
